﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Web.UI;
 


/// <summary>
/// Summary description for HelpControls
/// </summary>
public class HelpControls
{
    public static void ShowMsgBox(Page pParent, string StrText)
    {
        string strScript = "alert (\'" + StrText + "\');";

        ScriptManager.RegisterStartupScript(pParent, pParent.GetType(), null, strScript, true);
        //string strScript;
        //strScript = " <script language=JavaScript> ";
        //strScript += "alert(\"" + StrText + "\");";
        //strScript += "</script>";
        //pParent.RegisterStartupScript("test", strScript);
    }
    public static void ShowMsgBoxConfirm(Page pParent, string StrText)
    {
        string strScript;
        strScript = " <script language=JavaScript> ";
        strScript += "if (confirm(\"" + StrText + "?\") == true) return true; else return false;";
        strScript += "</script>";
        pParent.RegisterStartupScript("test", strScript);
    }
    public static void ShowMsgBoxConfirmDelete(Page pParent)
    {
        ShowMsgBoxConfirm(pParent, "Bạn có chắc là muốn xóa dữ liệu này?");
    }
    public static void InitDropDownList(DropDownList ComboboxName, DataTable datasource, string DataValueField, string DataTextField)
    {
        if (datasource == null || DataValueField == null)
        {
            ComboboxName.Items.Add(new ListItem("[Chưa có dữ liệu]", "-1"));
            return;
        }
        try
        {
            DataRow emptyRow=datasource.NewRow();
            emptyRow[DataValueField]=-1;
            emptyRow[DataTextField] = datasource.Rows.Count == 0 ? "[Chưa có dữ liệu]" : "[Chọn giá trị]";
            datasource.Rows.InsertAt(emptyRow, 0);
            ComboboxName.DataSource = null;
            ComboboxName.Items.Clear();
            if (DataValueField == "")
            {
                if (datasource.Columns.Count > 1)
                {
                    ComboboxName.DataValueField = datasource.Columns[0].ColumnName.ToString();
                    ComboboxName.DataTextField = datasource.Columns[1].ColumnName.ToString();
                }
                else
                {
                    ComboboxName.DataValueField = datasource.Columns[0].ColumnName.ToString();
                    ComboboxName.DataTextField = datasource.Columns[0].ColumnName.ToString();
                }
            }
            else
            {
                ComboboxName.DataValueField = datasource.Columns[DataValueField].ColumnName.ToString();
                ComboboxName.DataTextField = datasource.Columns[DataTextField].ColumnName.ToString();
            }
            ComboboxName.DataSource = datasource;
            ComboboxName.SelectedIndex = -1;
            ComboboxName.DataBind();
        }
        catch (Exception)
        {
        }
    }
    public static void InitListTinhThanh(DropDownList ComboboxName)
    {
        string sql = string.Format(@"SELECT * FROM {0} tt
                     ORDER BY isnull(tt.{1},9999), {2}", TINH_THANH.sTableName, TINH_THANH.cl_SAP_XEP,TINH_THANH.cl_TEN_TINH_THANH);
        DataTable dt = SQLConnectWeb.GetTable(sql, TINH_THANH.sTableName);
        InitDropDownList(ComboboxName, dt,  TINH_THANH.cl_TINH_THANH_ID, TINH_THANH.cl_TEN_TINH_THANH);

    }
    public static void InitListLoaiVanPhong(DropDownList ComboboxName)
    {
        string sql = string.Format(@"SELECT * FROM {0} tt
                     ORDER BY tt.{1}", LOAI_VAN_PHONG.sTableName, LOAI_VAN_PHONG.cl_LOAI_VP_ID);
        DataTable dt = SQLConnectWeb.GetTable(sql, LOAI_VAN_PHONG.sTableName);
        InitDropDownList(ComboboxName, dt, LOAI_VAN_PHONG.cl_LOAI_VP_ID, LOAI_VAN_PHONG.cl_TEN_LOAI_VP);

    }
    public static void InitListTuyenDuong(DropDownList ComboboxName)
    {
        string sql = string.Format(@"SELECT td.{0}, td.{1} +' <=> '+td.{2} TUYEN_DUONG_NAME
            FROM {3} td
            ORDER BY td.{1}, td.{2}", TUYEN_DUONG.cl_TUYEN_DUONG_ID, TUYEN_DUONG.cl_BEN_DI, TUYEN_DUONG.cl_BEN_DEN, TUYEN_DUONG.sTableName);
        DataTable dt = SQLConnectWeb.GetTable(sql, TUYEN_DUONG.sTableName);
        InitDropDownList(ComboboxName, dt, TUYEN_DUONG.cl_TUYEN_DUONG_ID, "TUYEN_DUONG_NAME");

    }
  
    public static void InitListLoaiXe(DropDownList ComboboxName)
    {
        string sql = string.Format(@"SELECT * FROM {0} tt
                     ORDER BY tt.{1}", LOAI_XE.sTableName, LOAI_XE.cl_LOAI_XE_ID);
        DataTable dt = SQLConnectWeb.GetTable(sql, LOAI_XE.sTableName);
        InitDropDownList(ComboboxName, dt, LOAI_XE.cl_LOAI_XE_ID, LOAI_XE.cl_TEN_LOAI);

    }
    public static void InitListLoaiBaiViet(DropDownList ComboboxName)
    {
        string sql = string.Format(@"SELECT * FROM {0} tt
                     ORDER BY {1}", LOAI_BAI_VIET.sTableName, LOAI_BAI_VIET.cl_TEN_LOAI);
        DataTable dt = SQLConnectWeb.GetTable(sql, LOAI_BAI_VIET.sTableName);
        InitDropDownList(ComboboxName, dt, LOAI_BAI_VIET.cl_LOAI_BAI_VIET_ID, LOAI_BAI_VIET.cl_TEN_LOAI);

    }
    public static string GetValueDDL(DropDownList ddl)
    {
        if (string.IsNullOrEmpty(ddl.SelectedValue) || ddl.SelectedValue == "-1") return "";
        return ddl.SelectedValue;
    }
    public static void SetValueDDL(DropDownList ddl,string value)
    {
        try
        {
            if (string.IsNullOrEmpty(value))
                value = "-1";
            ddl.SelectedValue = value;
        }
        catch { }
    }
}